<template>
  <el-drawer v-model="drawerVisible" title="搜索" direction="rtl" size="350px">
    <template #header>
      <span class="text-16px font-700">搜索</span>
    </template>
    <div class="search-drawer" style="height: 100%">
      <el-form
        ref="queryFormRef"
        :inline="true"
        :model="queryParams"
        class="-mb-15px"
        label-width="68px"
      >
        <el-form-item label="模板名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入模板名称"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="模板编码" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入模板编码"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="发送的邮箱账号编号" prop="accountId">
          <el-input
            v-model="queryParams.accountId"
            placeholder="请输入发送的邮箱账号编号"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="发送人名称" prop="nickname">
          <el-input
            v-model="queryParams.nickname"
            placeholder="请输入发送人名称"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="模板标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入模板标题"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="参数数组" prop="params">
          <el-input
            v-model="queryParams.params"
            placeholder="请输入参数数组"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="开启状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择开启状态"
            clearable
            class="!w-240px"
          >
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="queryParams.remark"
            placeholder="请输入备注"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-220px"
          />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="search">搜索</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
  import { computed, ref } from 'vue'

  // 定义 props 和 emits
  const props = defineProps({
    modelValue: {
      type: Object,
      required: true,
    }
  });

  const emit = defineEmits(['update:modelValue', 'search', 'reset']);

  // 控制抽屉显示
  const drawerVisible = ref(false);

  // 使用 computed 实现双向绑定
  const queryParams = computed({
    get: () => props.modelValue,
    set: (value) => emit('update:modelValue', value)
  });

  // 打开抽屉
  function open() {
    drawerVisible.value = true;
  }

  // 关闭抽屉
  function close() {
    drawerVisible.value = false;
  }

  // 搜索
  function search() {
    close();
    emit('search', queryParams.value);
  }

  const queryFormRef = ref()
  // 重置
  function reset() {
    queryFormRef.value.resetFields()
    emit('reset', queryParams.value);
  }

  // 暴露 open 方法
  defineExpose({
    open
  });
</script>

<style lang="scss" scoped>
  .search-drawer {
    ::v-deep {
      .el-form {
        height: 98%;
      }
    }
  }
</style>